﻿using AppData;
using Microsoft.AspNetCore.Mvc;
using System;
using ServerShared;
using AppData.DTO;
using System.Security.Policy;
using System.Xml.Linq;
using Google.Protobuf.WellKnownTypes;

namespace AppServer.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class BooksController : ControllerBase
    {
        [HttpGet("get_recommended_books")]
        public ActionResult<string> get_recommended_books(int num)
        //public IEnumerable<Book> get_recommended_books(int num)
        {
            GameDebug.Log($"get_recommended_books：{num}");

            List<Book> books = new List<Book>();
            books.AddRange(TableHelper.GetAllBooks(num));//BooksSaver.GetAllBooks(enumValue);

            return Newtonsoft.Json.JsonConvert.SerializeObject(books);
        }

        [HttpGet( "get_books_with_name")]
        public ActionResult<string> get_books_with_name(string name)
            //public IEnumerable<Book> get_recommended_books(int num)
        {
            GameDebug.Log($"get_books_with_name：{name}");

            int num = 1000;

            List<Book> books = new List<Book>();
            for (int i = (int)SqlTable.books_A; i <= (int)SqlTable.books_Z; i++)
            {
                var genreBooks = TableHelper.GetBooksWithName((SqlTable)i,name, num);//BooksSaver.GetAllBooks(enumValue);

                foreach (var book in genreBooks)
                {
                    books.Add(book);

                    if (books.Count >= num)
                    {
                        return Newtonsoft.Json.JsonConvert.SerializeObject(books);
                    }
                }
            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(books);
        }

        /// <summary>
        /// 上传书籍
        /// </summary>
        /// <param name="maintainData"></param>
        /// <returns></returns>
        [HttpPost("upload_book_with_str")]
        public ActionResult<string> upload_book_with_str(string book_json)
        {
            GameDebug.Log($"上传书 with str：{book_json}");

            if (string.IsNullOrEmpty(book_json))
            {
                GameDebug.LogError($"book_json is null");
                return BadRequest();
            }
            BookProto book = Newtonsoft.Json.JsonConvert.DeserializeObject<BookProto>(book_json);
            return upload_book(book);
        }
        /// <summary>
        /// 上传书籍
        /// </summary>
        /// <param name="maintainData"></param>
        /// <returns></returns>
        [HttpPost("upload_book")]
        public ActionResult<string> upload_book(BookProto book)
        {
            GameDebug.Log($"上传书：{book?.name} {book?.author} ");

            if (book == null || string.IsNullOrEmpty(book.name))
            {
                GameDebug.LogError($"图书名字不能为空");
                return BadRequest();
            }

            string genre = books.GetBookGenre(book.name);

            long id = books.AddBook(book.name, book.sub_genre, book.description,
                book.author, book.publisher, book.publishDate, book.fileExt);

            //if (id <= 0)
            //{
            //    GameDebug.LogError($"图书已存在");
            //    return BadRequest();
            //}

            if (book.contentData != null && book.contentData.Length > 0)
            {
                int ret = BooksSaver.SaveBook(genre, id, book.fileExt, book.contentData);

                if (ret < 0)
                {
                    GameDebug.LogError($"保存图书文件失败：{id}  {book.contentData != null}");
                    return BadRequest();
                }
            }

            Tex tex = null;
            if (book.tex != null && book.tex.Length > 0)
            {
                TextureSaver.SaveBookTexture(id, genre, /*book.texWidth, book.texHeight,*/ book.tex);
                tex = new Tex(book.tex /*, book.texWidth, book.texHeight*/);
            }

            Book bookRet = new Book()
            {
                id =id,
                info = new BookInfo(book.sub_genre, book.name, book.description, book.author, book.publisher, book.publishDate, book.fileExt, tex),
                contentData = book.contentData
            };

            return Newtonsoft.Json.JsonConvert.SerializeObject(bookRet);
        }
    }
}
